package server;

import java.io.InputStream;
import java.net.Socket;

/**
 * @author chengzhj
 * @data 2021/9/25 9:58 下午
 */
public class RequestProcessorMapper extends Thread {

    private Socket socket;
    private Mapper mapper;

    public RequestProcessorMapper(Socket socket, Mapper mapper) {
        this.socket = socket;
        this.mapper = mapper;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            if (mapper.findByHostContext(request.getHostName(), request.getUrl()) == null) {
                response.outputHtml(request.getUrl());
            } else {
                // 如果在Mapper中找到该url，就用mapper中的
                HttpServlet httpServlet = mapper.findByHostContext(request.getHostName(), request.getUrl());
                httpServlet.service(request, response);
            }

            response.outputHtml(request.getUrl());
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
